package algorithm;

public class T00097 {
    public static void main(String[] args) {
        String s1 = "aabc";
        String s2 = "abad";
        String s3 = "aabcabad";
        System.out.println(new Solution().isInterleave(s1, s2, s3));
    }

    static class Solution {
        public boolean isInterleave(String s1, String s2, String s3) {
            int l1 = s1.length();
            int l2 = s2.length();
            int l3 = s3.length();
            if (l3 == 0 && l1 == 0 && l2 == 0) return true;
            if (l3 != l1 + l2) return false;
            int i1 = 0, i2 = 0, i3 = 0;
            while (i3 < l3) {
                char c = s3.charAt(i3);
                if (i2 < l2 && s2.charAt(i2) == c) {
                    i2 ++;
                } else if (i1 < l1 && s1.charAt(i1) == c) {
                    i1 ++;
                } else {
                    return false;
                }
                i3 ++;
            }
            return i3 == l3;
        }
    }
}
